package com.tx.user.server.controller;

import com.tx.user.server.constant.ConstantCookie;
import com.tx.user.server.constant.RedisConstant;
import com.tx.user.server.dataobject.UserInfo;
import com.tx.user.server.enums.ResultEnum;
import com.tx.user.server.enums.RoleEnum;
import com.tx.user.server.service.UserService;
import com.tx.user.server.utils.CookieUtil;
import com.tx.user.server.utils.ResultVOUtil;
import com.tx.user.server.vo.ResultVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 买家登陆
     * @param openid
     * @param response
     * @return
     */
    @GetMapping("/buyer")
    public ResultVO buyer(@RequestParam("openid") String openid,
                          HttpServletResponse response){
        // 1.openid和数据库中的数据是否匹配
        UserInfo userInfo = userService.findByOpenid(openid);
        if (userInfo == null) {
            return ResultVOUtil.error(ResultEnum.LOGIN_FAIL);
        }
        // 2.判断角色
        if (RoleEnum.BUYER.getCode()!=userInfo.getRole()) {
            return ResultVOUtil.error(ResultEnum.ROLE_ERROR);
        }
        // 3.cookie里设置openid=abc
        CookieUtil.set(response, ConstantCookie.OPENID, openid, ConstantCookie.expire);

        return ResultVOUtil.success();
    }

    /**
     * 卖家登陆
     * @param openid
     * @param response
     * @return
     */
    @GetMapping("/seller")
    public ResultVO seller(@RequestParam("openid") String openid,
                          HttpServletRequest request,
                           HttpServletResponse response){
        // 判断是否已经登陆
        Cookie cookie = CookieUtil.get(request, ConstantCookie.TOKEN);
        // cookie不为空并且redis中的数据值不为空
        if (cookie !=null &&
                !StringUtils.isEmpty(stringRedisTemplate.opsForValue().get(
                        String.format(RedisConstant.TOKEN_TEMPLATE, cookie.getValue())
                ))) {
            return ResultVOUtil.success();
        }

        // 1.openid和数据库中的数据是否匹配
        UserInfo userInfo = userService.findByOpenid(openid);
        if (userInfo == null) {
            return ResultVOUtil.error(ResultEnum.LOGIN_FAIL);
        }
        // 2.判断角色
        if (RoleEnum.SELLER.getCode()!=userInfo.getRole()) {
            return ResultVOUtil.error(ResultEnum.ROLE_ERROR);
        }
        // 3.reids设置key=UUID, value=xyz
        String token = UUID.randomUUID().toString();
        Integer expire = ConstantCookie.expire;
        stringRedisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_TEMPLATE,
                token),
                openid,
                // 过期时间
                expire,
                // 过期时间单位
                TimeUnit.SECONDS);
        // 4.cookie里设置token=uuid
        CookieUtil.set(response, ConstantCookie.TOKEN, token, ConstantCookie.expire);

        return ResultVOUtil.success();
    }

}
